package com.dyh.sentinel.demos.client1;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/*
@Author=Joey Wheeler
@Date=2020/12/7 11:32
*/
public class Demo {

    /*QPS 20 的规则*/
    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    /*
    ~/logs/csp/
    C:\Users\MSI-msi\logs\csp

    com-dyh-sentinel-demos-client1-Demo-metrics.log.2020-12-07

    其中 p 代表通过的请求,
    block 代表被阻止的请求,
    s 代表成功执行完成的请求个数,
    e 代表用户自定义的异常,
    rt 代表平均响应时长
    |--timestamp-|------date time----|-resource-|p |block|s |e|rt
    1607312080000|2020-12-07 11:34:40|HelloWorld|20|9802|20|0|0|0|0|0
    1607312081000|2020-12-07 11:34:41|HelloWorld|20|49948|20|0|0|0|0|0
    1607312082000|2020-12-07 11:34:42|HelloWorld|20|99479|20|0|0|0|0|0


    1607312606000|2020-12-07 11:43:26|HelloWorld|1|0|0|0|0|0|0|0
    1607312607000|2020-12-07 11:43:27|HelloWorld|3|0|3|0|338|0|0|0
    1607312608000|2020-12-07 11:43:28|HelloWorld|2|0|2|0|650|0|0|0
    1607312609000|2020-12-07 11:43:29|HelloWorld|2|0|2|0|450|0|0|0
    1607312610000|2020-12-07 11:43:30|HelloWorld|2|0|2|0|450|0|0|0
    1607312611000|2020-12-07 11:43:31|HelloWorld|1|0|1|0|402|0|0|0
    1607312612000|2020-12-07 11:43:32|HelloWorld|2|0|2|0|800|0|0|0
    1607312613000|2020-12-07 11:43:33|HelloWorld|1|0|1|0|700|0|0|0
    1607312614000|2020-12-07 11:43:34|HelloWorld|3|0|3|0|500|0|0|0
    1607312615000|2020-12-07 11:43:35|HelloWorld|1|0|1|0|701|0|0|0

    1607312965000|2020-12-07 11:49:25|HelloWorld|2|0|1|0|60|0|0|0
1607312966000|2020-12-07 11:49:26|HelloWorld|18|0|18|0|54|0|0|0
1607312967000|2020-12-07 11:49:27|HelloWorld|17|0|17|0|56|0|0|0
1607312968000|2020-12-07 11:49:28|HelloWorld|20|4682|21|0|46|0|0|0
1607312969000|2020-12-07 11:49:29|HelloWorld|20|6030|20|0|45|0|0|0



    */
    public static void main(String[] args) {
        initFlowRules();

        Random random = new Random();

        while (true) {
            Entry entry = null;
            try {
                entry = SphU.entry("HelloWorld");
                /*您的业务逻辑 - 开始*/
                Thread.sleep(   10* random.nextInt(10));

                System.out.println("hello world");
                /*您的业务逻辑 - 结束*/
            }
            catch (InterruptedException e){
                System.out.println(e);
            }
            catch (BlockException e) {
                /*流控逻辑处理 - 开始*/
                System.out.println("block!");
                /*流控逻辑处理 - 结束*/
            } finally {
                if (entry != null) {
                    entry.exit();
                }
            }
        }
    }
}
